python+wxauto实现微信助手 您所在的位置:网站首页 python 微信api python+wxauto实现微信助手

python+wxauto实现微信助手

2024-01-03 07:11| 来源: 网络整理| 查看: 265

前言

之前一直太忙,老是不能及时回复朋友的信息,所以就想着做一个可以自动回复朋友的机器人,查了许多资料,发现itachat只能写网页版,但微信没有网页版了,也看了itchat-uos,但仅支python2.7和python3.5,但我是python3.7,所以。。。(此处是我的痛),所以我选择wxauto,还有一个技术是基于hook的,可以看这篇文章基于hook的python机器人,彻底取代itchat,还有wxpy,有兴趣的朋友可自行查阅。

如何安装wxauto

使用Pycharm的库自行导入

from wxauto import *

或者使用pip安装wxauto,只需要在终端入以下代码:

pip install wxauto  启动微信客户端

扫码登录你的微信客户端,我还使用了天行数据的api接口,来实现定时发送晚安心语,早上每日一言。注册天行数据在下面的data中填写你自己的key

import requests from wxauto import * from urllib import request, parse import time import json class autoReply(): def __init__(self): # 获取当前微信客户端 self.wx = WeChat() # 天行数据接口格式 self.headers = { 'Content-type': 'application/x-www-form-urlencoded' } # 填写你自己的密钥 self.data = { "key": "you key", } 重写GetLastMessage方法

由于wxauto实现的功能太少,无法实现其自动回复,所以要重写GetLastMessage方法,还有wxauto其他的方法见文章GitHub - cluic/wxauto: Windows版本微信客户端(非网页版)自动化,可实现简单的发送、接收微信消息,持续更新中

def GetLast_Message(self, user): # 获取user的最新一条消息 i = -1 messages = self.wx.GetAllMessage while True: if messages[i][0] == user: message = messages[i] return message i = i - 1 实现定时发送 # 定时发送早上每日一言,晚安! def send_time(self, time1, time2): t = time.strftime('%H:%M:%S', time.localtime()) if t == time1: # 每日一言接口 url2 = 'https://api.xygeng.cn/one' data = parse.urlencode(self.data) req = request.Request(url=url2, headers=self.headers, data=bytes(data, encoding="utf-8")) res = request.urlopen(req) json_data = json.load(res) print(json_data) if json_data["code"] == 200: message = json_data['data']['content'] message = '每日一言:' + message self.wx.SendMsg(message) else: self.wx.SendMsg('每日一言') elif t == time2: # 天行数据的晚安心语接口 url1 = "https://apis.tianapi.com/wanan/index" data = parse.urlencode(self.data) req = request.Request(url=url1, headers=self.headers, data=bytes(data, encoding="utf-8")) res = request.urlopen(req) json_data = json.load(res) if json_data["code"] == 200: message = json_data['result']['content'] self.wx.SendMsg(message) else: self.wx.SendMsg('晚安') 最后实现回复信息 def reply_message(self, user): #机器人接口 url = 'https://v.api.aa1.cn/api/api-xiaoai/talk.php' # 打开user聊天框 self.wx.ChatWith(user) #获取user的最后一条信息 a = self.GetLast_Message(user) b = 0 while True: # 定时发送 time1 = '08:00:00' # 早上时间 time2 = '22:00:00' # 晚上时间 self.send_time(time1, time2) # 自动回复 message = self.GetLast_Message(user) # 修改自动回复的第一句 if message != a and b != 0: res = requests.get(url=url, params="msg=" + message[1]) self.wx.SendMsg(res.text) a = message elif message != a and b == 0: self.wx.SendMsg("主人不在,有什么问题我可以为你解决哦!") b=b+1 if __name__ == '__main__': x = autoReply() # 输入你要自动回复的朋友的名字 x.reply_message('The quiet coder') 总结

之前本来就只想做个自动回复算了,但看代码就几行解决了,所以就多做了每日一言,晚安,如果觉得多余的话,把send_time方法删除就行了,由于wxauto实现的功能实在是太少了,所以只能实现对某个人的自动回复,我也尝试过使用wxauto底层的代码方法去实现,但太难了,所以就打住了,若你们有更好的方法实现全部的自动回复的话,希望在下面的评论区留下你的见解。

最后资料整理不易,不奢求能进各位的收藏夹,只希望各位大佬们能留个赞👍!!!

更新:

由于微信版本更新的原因,会出现LookupError: Find Control Timeout(10.0s): {Name: '输入', ControlType: EditControl}问题,解决方案如下:wxauto发送功能失效出现LookupError: Find Control Timeout(10.0s): {Name: ‘输入‘, ControlType: EditControl}解决_The quiet coder的博客-CSDN博客https://blog.csdn.net/m0_65783113/article/details/132524827



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有